package com.bootdo.system.filter;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.R;
import com.bootdo.common.wxpay.MD5Util;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 /**
  *
  * 参数清洗
 　　* @author 郝江波
 　　* @date 2019/10/26 17:16
 　　*/
 //@Component
 public class HjbvisaFilter implements Filter {

  
    @Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
    	String hjbtime = req.getParameter("hjbtime");
		String hjbvisa = req.getHeader("hjbvisa");

		if(StringUtils.isNotEmpty(hjbvisa)){
			Map<String, String[]> par =  req.getParameterMap();
			String p = "";
			if(null != par) {
				int i = 0;
				for(Map.Entry<String, String[]> entry : par.entrySet()) {
					String vstr = Arrays.toString(entry.getValue());
					//System.err.println(vstr);
					String v = URLEncoder.encode(vstr.substring(1,vstr.length()-1),"utf-8");
					v = v.replaceAll("\\+", "%20");
					String k =  entry.getKey();
					k = k.replaceAll("\\[","%5B");
					k = k.replaceAll("\\]","%5D");
					p += k +"=" + v;
					if(i < par.entrySet().size() - 1){
						p+="&";
					}
					i++;
				}
			}
			//System.err.println("前端时间："+hjbtime);
			//System.err.println("前端签名："+hjbvisa);
			//System.err.println(p);
			//System.err.println("后端签名："+ MD5Util.MD5Encode(p+hjbtime+"HJB","utf-8"));
			String sgin =  MD5Util.MD5Encode(p+hjbtime+"HJB","utf-8");
			if(sgin.equals(hjbvisa)){
				filterChain.doFilter(request, response);
			}else{
				if("ok".equals(req.getParameter("fwb"))){
					filterChain.doFilter(request, response);
				}
				staticWriteJson(req,res, R.error(401, "验证失败"));
			}
		}else{
			if(req.getRequestURI().contains("/f/")){
				if(req.getRequestURI().contains("/f/file/common/")){
					filterChain.doFilter(request, response);
				}else if(req.getRequestURI().contains("/f/wxcallback")){
					filterChain.doFilter(request, response);
				}else if(req.getRequestURI().contains("/f/activity/getOrderQrcode")){
					filterChain.doFilter(request, response);
				}
				else{
					staticWriteJson(req,res, R.error(400, "非法请求"));
				}
			}else if(req.getRequestURI().contains("/common/sysFile/upload")){
				filterChain.doFilter(request, response);
			}
			 else{
				filterChain.doFilter(request, response);
			}

		}
    }

	 private static void staticWriteJson( HttpServletRequest request, HttpServletResponse response,Object object){
		 response.setContentType("application/json; charset=UTF-8");
		 try {
			 PrintWriter out = response.getWriter();
			 String jsonString = JSON.toJSON(object).toString();
			 out.print(jsonString);
			 out.flush();
			 out.close();
		 } catch (Exception e) {

			 e.printStackTrace();
		 }
	 }

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void destroy() {}
  
}  
